﻿namespace VIT.BusinessLogicLayer
{
    using System.Collections.Generic;
    using System.Linq;

    using VIT.DataAccessLayer;
    using VIT.DataTransferObjects.Presentation;
    using VIT.Library;

    public class ColorStyleBLL : BLLBase
    {
        private readonly IColorDAL _colorDAL;
        private readonly IStyleDAL _styleDAL;
        private readonly IProductDAL _productDAL;

        private readonly string _domainStore;
        private readonly string _colorImageThumbPath;
        private readonly string _colorImageDetailPath;
        private readonly string _styleImageThumbPath;
        private readonly string _styleImageDetailPath;

        #region Constructor

        public ColorStyleBLL(string connectionString = "")
            : base(connectionString)
        {
            this._colorDAL = new ColorDAL(this.DatabaseFactory);
            this._styleDAL = new StyleDAL(this.DatabaseFactory);
            this._productDAL = new ProductDAL(DatabaseFactory);

            this._colorImageThumbPath = SettingsManager.AppSettings.ColorImageThumbPath;
            this._colorImageDetailPath = SettingsManager.AppSettings.ColorImageDetailPath;
            this._styleImageThumbPath = SettingsManager.AppSettings.StyleImageThumbPath;
            this._styleImageDetailPath = SettingsManager.AppSettings.StyleImageDetailPath;
            this._domainStore = SettingsManager.AppSettings.DomainStore;
        }
        #endregion

        #region Color
        public List<ColorDto> GetAllColor(int companyId)
        {
            var dtoQuery = _colorDAL.GetAllIncludeItem()
                .Where(color => color.Item.IsPublished && color.Item.CompanyId == companyId)
                .Select(color => new ColorDto
                {
                    Id = color.Id,
                    Description = color.Description,
                    Name = color.Name,
                    Value = color.Value,
                    ImageName = color.ImageName
                });

            var result = dtoQuery.ToList();

            // update path image
            var colorImageThumbPath = string.Format(this._colorImageThumbPath, companyId);
            var colorImageDetailPath = string.Format(this._colorImageDetailPath, companyId);
            foreach (var dto in result)
            {
                dto.ImageThumbPath = this.VirtualPath(colorImageThumbPath, dto.ImageName);
                dto.ImageDetailPath = this.VirtualPath(colorImageDetailPath, dto.ImageName);
            }

            return result;
        }
        public List<ColorCountProductDto> GetColorCountProduct()
        {
            var query = _colorDAL.GetAllIncludeItem()
                .Where(color => color.Item.IsPublished)
                .OrderBy(c => c.Item.Orders)
                .Select(c => new ColorCountProductDto
                    {
                        Id = c.Id,
                        Name = c.Name,
                        Value = c.Value,
                        CountProduct = _productDAL.GetMany(products => products.Color.Contains(c.Id.ToString())).Count()
                    });

            var data = query.ToList();

            return data;
        }
        public List<ColorCountProductDto> GetColorCountProductByCategiry(List<int> listCategory, string languageId)
        {
            var list = _colorDAL.GetAllIncludeItem()
                .Where(color => color.Item.IsPublished)
                .OrderBy(c => c.Item.Orders)
                .Select(c => new ColorCountProductDto
                {
                    Id = c.Id,
                    Name = c.Name,
                    Value = c.Value,
                    CountProduct = 0
                }).ToList();

            foreach (var co in list)
            {
                var color = co.Id.ToString();
                var lstColorString = _productDAL.GetAllIncludeItem().Where(c => listCategory.Contains(c.CategoryId) && c.Item.HasDeleted == false && c.Item.IsPublished && c.Color!=null).Select(c => c.Color).ToList().Select(c => c.Split(',')).ToList();

                co.CountProduct = lstColorString.Count(c => c.Contains(color));
            }
            return list;
        }

        public ColorDto GetColorById(int colorId, int companyId)
        {
            var dtoQuery = _colorDAL.GetAllIncludeItem()
                .Where(color => color.Id == colorId && color.Item.IsPublished && color.Item.CompanyId == companyId)
                .Select(color => new ColorDto
                {
                    Id = color.Id,
                    Description = color.Description,
                    Name = color.Name,
                    Value = color.Value,
                    ImageName = color.ImageName
                });

            var dto = dtoQuery.FirstOrDefault();
            if (dto != null)
            {
                var colorImageThumbPath = string.Format(this._colorImageThumbPath, companyId);
                var colorImageDetailPath = string.Format(this._colorImageDetailPath, companyId);
                dto.ImageThumbPath = this.VirtualPath(colorImageThumbPath, dto.ImageName);
                dto.ImageDetailPath = this.VirtualPath(colorImageDetailPath, dto.ImageName);
            }

            return dto;
        }

        public List<ColorDto> GetColorByIds(List<int> colorIds, int companyId)
        {
            var dtoQuery = _colorDAL.GetAllIncludeItem()
                .Where(color => colorIds.Contains(color.Id) && color.Item.IsPublished && color.Item.CompanyId == companyId)
                .Select(color => new ColorDto
                {
                    Id = color.Id,
                    Description = color.Description,
                    Name = color.Name,
                    Value = color.Value,
                    ImageName = color.ImageName
                });

            var dto = dtoQuery.ToList();

            // update path image
            var colorImageThumbPath = string.Format(this._colorImageThumbPath, companyId);
            var colorImageDetailPath = string.Format(this._colorImageDetailPath, companyId);
            foreach (var o in dto)
            {
                o.ImageThumbPath = this.VirtualPath(colorImageThumbPath, o.ImageName);
                o.ImageDetailPath = this.VirtualPath(colorImageDetailPath, o.ImageName);
            }
            
            return dto;
        }
        #endregion

        #region Style
        public List<StyleDto> GetAllStyle(int companyId)
        {
            var dtoQuery = _styleDAL.GetAllIncludeItem()
                .Where(style => style.Item.IsPublished && style.Item.CompanyId == companyId)
                .OrderBy(style => style.Item.Orders)
                .Select(style => new StyleDto
                {
                    Id = style.Id,
                    Description = style.Description,
                    Name = style.Name,
                    ImageName = style.ImageName
                });

            var result = dtoQuery.ToList();

            // update path image
            var styleImageThumbPath = string.Format(this._styleImageThumbPath, companyId);
            var styleImageDetailPath = string.Format(this._styleImageDetailPath, companyId);
            foreach (var dto in result)
            {
                dto.ImageThumbPath = this.VirtualPath(styleImageThumbPath, dto.ImageName);
                dto.ImageDetailPath = this.VirtualPath(styleImageDetailPath, dto.ImageName);
            }

            return result;
        }

        public StyleDto GetStyleById(int styleId, int companyId)
        {
            var dtoQuery = _styleDAL.GetAllIncludeItem()
                .Where(style => style.Id == styleId && style.Item.IsPublished && style.Item.CompanyId == companyId)
                .Select(style => new StyleDto
                {
                    Id = style.Id,
                    Description = style.Description,
                    Name = style.Name,
                    ImageName = style.ImageName
                });

            var dto = dtoQuery.FirstOrDefault();
            if (dto != null)
            {
                var styleImageThumbPath = string.Format(this._styleImageThumbPath, companyId);
                var styleImageDetailPath = string.Format(this._styleImageDetailPath, companyId);

                dto.ImageThumbPath = this.VirtualPath(styleImageThumbPath, dto.ImageName);
                dto.ImageDetailPath = this.VirtualPath(styleImageDetailPath, dto.ImageName);
            }

            return dto;
        }

        public List<StyleDto> GetStyleByIds(List<int> styleIds, int companyId)
        {
            var dtoQuery = _styleDAL.GetAllIncludeItem()
                .Where(style => styleIds.Contains(style.Id) && style.Item.IsPublished && style.Item.CompanyId == companyId)
                .Select(color => new StyleDto
                {
                    Id = color.Id,
                    Description = color.Description,
                    Name = color.Name,
                    ImageName = color.ImageName
                });

            var dto = dtoQuery.ToList();

            // update path image
            var styleImageThumbPath = string.Format(this._styleImageThumbPath, companyId);
            var styleImageDetailPath = string.Format(this._styleImageDetailPath, companyId);
            foreach (var o in dto)
            {
                o.ImageThumbPath = this.VirtualPath(styleImageThumbPath, o.ImageName);
                o.ImageDetailPath = this.VirtualPath(styleImageDetailPath, o.ImageName);
            }

            return dto;
        }
        #endregion

        public IList<ColorStyleDto> GetAllColorStyle()
        {
            var list = _colorDAL.GetAllIncludeStyle().ToList();
            var result = (from color in list
                          from style in color.Styles
                          select new ColorStyleDto
                          {
                              ColorId = color.Id,
                              ColorName = color.Name,
                              StyleId = style.Id,
                              StyleName = style.Name
                          });

            return result.ToList();
        }

        public List<ColorStyleDto> GetColorStyleByColor(int colorId)
        {
            var list = _colorDAL.GetAllIncludeStyle()
                .Where(color => color.Id == colorId)
                .ToList();

            var result = (from color in list
                          from style in color.Styles
                          select new ColorStyleDto
                          {
                              ColorId = color.Id,
                              ColorName = color.Name,
                              StyleId = style.Id,
                              StyleName = style.Name
                          });

            return result.ToList();
        }
        public List<ColorStyleDto> GetColorStyleByStyle(int styleId)
        {
            var list = _styleDAL.GetAllIncludeColor()
                .Where(color => color.Id == styleId)
                .ToList();

            var result = (from style in list
                          from color in style.Colors
                          select new ColorStyleDto
                          {
                              ColorId = color.Id,
                              ColorName = color.Name,
                              StyleId = style.Id,
                              StyleName = style.Name
                          });

            return result.ToList();
        }

        #region Private method
        private string VirtualPath(string folderPath, string fileName)
        {
            return string.IsNullOrEmpty(fileName) ? string.Empty : string.Format("{0}{1}{2}", this._domainStore, folderPath, fileName);
        }
        #endregion
    }
}
